package com.dormitory.myt.sushecontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dormitory.myt.common.R;
import com.dormitory.myt.en.Repair;
import com.dormitory.myt.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/repair")

public class RepairController {

    @Autowired
   private RepairService repairService;
    /**
     * 学生端留宿申请
     * @param response
     * @param sushehao
     * @param floor
     * @param content
     * @param reporttime
     * @return
     */
    @GetMapping("/up")
    public R<String> xsxs(HttpServletResponse response, @RequestParam Integer sushehao, @RequestParam String floor, @RequestParam String content, @RequestParam String reporttime){
        response.setHeader("Access-Control-Allow-Origin","*");
        Repair r=new Repair();
        r.setSushehao(sushehao);
        r.setState(0);
        r.setFloor(floor);
        r.setContent(content);
        r.setReporttime(reporttime);
        repairService.save(r);  //可能一个专业有多个数据的风险，bug！
        return R.success("year");
    }

    /**
     * 学生端状态查询
     * @param response
     * @param sushehao
     * @return
     */
    @GetMapping("/state")
    public R<Repair> xsxs1(HttpServletResponse response,@RequestParam Integer sushehao){
        response.setHeader("Access-Control-Allow-Origin","*");

        LambdaQueryWrapper<Repair> queryWrapper = new LambdaQueryWrapper<>();
        //根据宿舍来查询：
        queryWrapper.eq(Repair::getSushehao,sushehao);
        List<Repair> list = repairService.list(queryWrapper);
        Repair l = list.get(0);
        return R.success(l);
    }
    //查询报修信息:
    @GetMapping()
    public R<List> page(){

        LambdaQueryWrapper<Repair> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByAsc(Repair::getReporttime);

        List<Repair> list=repairService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 宿管点击处理
     * @param response
     * @param sushehao
     * @param clicktime
     * @return
     */
    @GetMapping("/chuli")
    public R<String> getLiusu2(HttpServletResponse response, @RequestParam int sushehao, @RequestParam String clicktime){
        response.setHeader("Access-Control-Allow-Origin","*");

        Repair l = new Repair();
        l.setSushehao(sushehao);
        l.setState(1);
        l.setClicktime(clicktime);
        LambdaUpdateWrapper<Repair> updateWrapper = Wrappers.<Repair>lambdaUpdate()
                .eq(Repair::getSushehao, l.getSushehao());
        repairService.update(l, updateWrapper);

        return R.success("year");
    }
}
